package com.bootdo.activity;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.repository.ProcessDefinition;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class 排他案例 {
    @Before
    public void testCreateTablesAutomaticallyWithoutConfigFiles() {
        // 创建流程引擎配置对象
        ProcessEngineConfiguration config = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
        // 设置jdbc连接参数信息
        config.setJdbcDriver("com.mysql.jdbc.Driver");
        config.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/new_frame_dubbo?useUnicode=true&characterEncoding=utf8");
        config.setJdbcUsername("root");
        config.setJdbcPassword("root");
        // 设置自动建表
        config.setDatabaseSchemaUpdate("true");
        // 使用配置对象创建流程引擎对象，创建对象过程中会自动建表
        ProcessEngine processEngine = config.buildProcessEngine();
    }

    /**
     * 查询所有的流程定义
     */
    @Test
    public void testQueryAllPD(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        List<ProcessDefinition> pdList = processEngine.getRepositoryService()
                .createProcessDefinitionQuery()
                .orderByProcessDefinitionVersion()
                .desc()
                .list();
        for (ProcessDefinition pd : pdList) {
            System.out.println(pd.getName() + pd.getVersion());
        }
    }



    /**
     * 根据pdid得到processDefinitionEntity
     */
    @Test
    public void testProcessDefinitionEntity(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        /**
         * 根据pdid得到ProcessDefinitionEntry
         */
        ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity)processEngine.getRepositoryService()
                .getProcessDefinition("myProcess_1:1:5004");

        System.err.println(processDefinitionEntity.getName());
    }


    /**
     * 根据pdid得到processDefinitionEntity中的activityimpl
     */
    @Test
    public void testGetActivityImpl() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        /**
         * 根据pdid得到ProcessDefinitionEntry
         */
        ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) processEngine.getRepositoryService()
                .getProcessDefinition("myProcess_1:1:5004");
        /**
         * ActivityImpl是一个对象
         * 一个activityImpl代表processDefinitionEntity中的一个节点
         */
        List<ActivityImpl> activityImpls = processDefinitionEntity.getActivities();
        for (ActivityImpl activityImpl : activityImpls) {
            System.out.println(activityImpl.getId());
            System.out.print("hegiht:" + activityImpl.getHeight());
            System.out.print("width:" + activityImpl.getWidth());
            System.out.print(" x:" + activityImpl.getX());
            System.out.println(" y:" + activityImpl.getY());
        }
    }




}
